﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExternalConnections.CUiPLCSimitCouplingConnectionForm
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.ExternalConnections.Res;
using EngineeringInternalExtension;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.Ui;
using UiInternalControls;


namespace DnProcessSimulateCommands.ExternalConnections;

public class CUiPLCSimitCouplingConnectionForm : TxForm
{
  private IApECExternalConnectionDataConvertor m_convertor = (IApECExternalConnectionDataConvertor) new CApECExternalConnectionDataEditConvertor();
  private CApECPLCSimitCouplingConnectionFormParams m_params;
  private int m_suspendEnablersCounter;
  private CApECExternalConnectionsManager m_dataMgr;
  private ToolTip m_infoTT = new ToolTip();
  private IContainer components;
  private Button m_okButton;
  private Label label1;
  private Label m_hostNameLabel;
  private TextBox m_name;
  private TextBox m_IP;
  private Button m_cancelButton;
  private Label label3;
  private Label m_prefixLabel;
  private TextBox m_prefix;
  private TxInfoButton m_infoBoxPrefix;
  private TextBox m_comElement;
  private TxInfoButton m_infoBoxComElement;
  private TxInfoButton m_infoBoxHostName;

  public CUiPLCSimitCouplingConnectionForm(CApECExternalConnectionsManager mgr)
  {
    this.InitializeComponent();
    this.m_dataMgr = mgr;
    this.m_infoTT.AutoPopDelay = 30000;
    this.m_infoTT.InitialDelay = 500;
    this.m_infoTT.ReshowDelay = 150;
    this.m_infoTT.ShowAlways = true;
    string name = new TxApplicationEx().Name;
    this.m_infoTT.SetToolTip((Control) this.m_infoBoxHostName, string.Format(CUiECExternalConnectionsResourceTable.SIMIT_COUPLING_HOST_NAME_TOOL_TIP, (object) name));
    this.m_infoBoxHostName.TooltipText = string.Format(CUiECExternalConnectionsResourceTable.SIMIT_COUPLING_HOST_NAME_TOOL_TIP, (object) name);
    this.m_infoTT.SetToolTip((Control) this.m_infoBoxPrefix, CUiECExternalConnectionsResourceTable.SIMIT_COUPLING_PREFIX_TOOL_TIP);
    this.m_infoBoxPrefix.TooltipText = CUiECExternalConnectionsResourceTable.SIMIT_COUPLING_PREFIX_TOOL_TIP;
    this.m_infoTT.SetToolTip((Control) this.m_infoBoxComElement, string.Format(CUiECExternalConnectionsResourceTable.SIMIT_COUPLING_COM_ELEMENT_TOOL_TIP, (object) name));
    this.m_infoBoxComElement.TooltipText = string.Format(CUiECExternalConnectionsResourceTable.SIMIT_COUPLING_COM_ELEMENT_TOOL_TIP, (object) name);
    this.EnalbeOKButton();
  }

  protected virtual void OnLoad(EventArgs e)
  {
    ((Control) this).MinimumSize = new Size(((Control) this).Width, ((Control) this).Height);
    ((Control) this).MaximumSize = new Size(int.MaxValue, ((Control) this).Height);
    base.OnLoad(e);
  }

  protected virtual void OnClosing(CancelEventArgs e)
  {
    if (((Form) this).DialogResult == DialogResult.OK)
      this.m_params.DataToApply = this.RetrieveDataFromUi();
    base.OnClosing(e);
  }

  internal DialogResult ShowDialog(
    CApECPLCSimitCouplingConnectionFormParams formParams)
  {
    this.InitMode(formParams);
    return ((Form) this).ShowDialog();
  }

  private void InitMode(
    CApECPLCSimitCouplingConnectionFormParams formParams)
  {
    this.m_params = formParams;
    this.SuspendUiEnablers();
    try
    {
      string str = string.Empty;
      switch (this.m_params.DialogMode)
      {
        case CApECPLCSimitCouplingConnectionFormParams.eMode.Edit:
          this.InitUiByData(this.m_params.ConnectionToEdit.Data, false, true);
          str = CUiECExternalConnectionsResourceTable.EDIT_SIMIT_COUPLING_CONNECTION_DLG_CAPTION;
          this.m_name.Focus();
          this.m_name.SelectAll();
          break;
        case CApECPLCSimitCouplingConnectionFormParams.eMode.Create:
          this.InitUiByData((ITxPlcExternalConnectionDataEx) new TxPlcSimitCouplingConnectionDataEx(), false, false);
          str = CUiECExternalConnectionsResourceTable.ADD_SIMIT_COUPLING_CONNECTION_DLG_CAPTION;
          this.m_name.Focus();
          break;
      }
      ((Control) this).Text = str;
    }
    finally
    {
      this.ResumeUiEnablers();
    }
  }

  private void InitUiByData(ITxPlcExternalConnectionDataEx data, bool updateEnablers, bool edit)
  {
    IApECExternalConnectionDataConvertor instance = this.m_params.DataConvertor.CreateInstance(data);
    if (!(CApECExternalConnectionsManager.GetConnectionTypeFromData(data) == CUiECExternalConnectionsResourceTable.SIMIT_COUPLING_TYPE))
      return;
    CApECExternalConnectionDataEditConvertor dataEditConvertor = (CApECExternalConnectionDataEditConvertor) instance;
    this.m_name.Text = dataEditConvertor.Name as string;
    this.m_IP.Text = dataEditConvertor.OpcServer as string;
    this.m_prefix.Text = dataEditConvertor.ItemHeader as string;
    if (edit && dataEditConvertor.COMElement != null)
      this.m_comElement.Text = dataEditConvertor.COMElement.ToString();
    if (!updateEnablers)
      return;
    this.UpdateAllEnablers();
  }

  private ITxPlcExternalConnectionDataEx RetrieveDataFromUi()
  {
    CApECExternalConnectionDataEditConvertor instance = (CApECExternalConnectionDataEditConvertor) this.m_convertor.CreateInstance((ITxPlcExternalConnectionDataEx) new TxPlcSimitCouplingConnectionDataEx());
    instance.Name = (object) this.m_name.Text;
    instance.COMElement = (object) this.m_comElement.Text;
    instance.OpcServer = (object) this.m_IP.Text;
    instance.ItemHeader = (object) this.m_prefix.Text;
    return instance.Data;
  }

  private void OK_Click(object sender, EventArgs e)
  {
    ((Form) this).DialogResult = DialogResult.OK;
    if (((Form) this).Modal)
      return;
    ((Form) this).Close();
  }

  private void Action_FormClosing(object sender, FormClosingEventArgs e)
  {
    if (((Form) this).DialogResult == DialogResult.OK)
    {
      ITxPlcExternalConnectionDataEx connectionDataEx = this.RetrieveDataFromUi();
      switch (this.m_params.DialogMode)
      {
        case CApECPLCSimitCouplingConnectionFormParams.eMode.Edit:
          CApECExternalConnectionsManager.eSetConnectionDataFailiureReason failiureReason1;
          if (!this.m_dataMgr.CanSetConnectionData(this.m_params.ConnectionToEdit, connectionDataEx, out failiureReason1))
          {
            e.Cancel = true;
            switch (failiureReason1)
            {
              case CApECExternalConnectionsManager.eSetConnectionDataFailiureReason.DuplicatedName:
                int num1 = (int) TxMessageBox.ShowModal(CUiECExternalConnectionsResourceTable.ERR_MSG_DUPLICATED_CONNECTION_NAME, ((Control) this).Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                break;
              case CApECExternalConnectionsManager.eSetConnectionDataFailiureReason.DuplicatedKey:
                int num2 = (int) TxMessageBox.ShowModal(CUiECExternalConnectionsResourceTable.ERR_MSG_DUPLICATED_KEY_SIMIT, ((Control) this).Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                break;
            }
          }
          else
            break;
          break;
        case CApECPLCSimitCouplingConnectionFormParams.eMode.Create:
          CApECExternalConnectionsManager.eCreateConnectionFailiureReason failiureReason2;
          if (!this.m_dataMgr.CanCreateConnectionWithData(connectionDataEx, out failiureReason2))
          {
            e.Cancel = true;
            switch (failiureReason2)
            {
              case CApECExternalConnectionsManager.eCreateConnectionFailiureReason.DuplicatedName:
                int num3 = (int) TxMessageBox.ShowModal(CUiECExternalConnectionsResourceTable.ERR_MSG_DUPLICATED_CONNECTION_NAME, ((Control) this).Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                break;
              case CApECExternalConnectionsManager.eCreateConnectionFailiureReason.DuplicatedKey:
                int num4 = (int) TxMessageBox.ShowModal(CUiECExternalConnectionsResourceTable.ERR_MSG_DUPLICATED_KEY_SIMIT, ((Control) this).Text, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                break;
            }
          }
          else
            break;
          break;
      }
      if (!e.Cancel)
        this.m_params.DataToApply = connectionDataEx;
      else
        ((Form) this).DialogResult = DialogResult.Cancel;
    }
    base.OnClosing((CancelEventArgs) e);
  }

  private void m_cancelButton_Click(object sender, EventArgs e)
  {
    ((Form) this).DialogResult = DialogResult.Cancel;
    if (((Form) this).Modal)
      return;
    ((Form) this).Close();
  }

  private void m_name_TextChanged(object sender, EventArgs e) => this.EnalbeOKButton();

  private void m_comElement_TextChanged(object sender, EventArgs e) => this.EnalbeOKButton();

  private bool EnablersSuspended() => this.m_suspendEnablersCounter > 0;

  private void SuspendUiEnablers() => ++this.m_suspendEnablersCounter;

  private void ResumeUiEnablers()
  {
    if (this.EnablersSuspended())
      --this.m_suspendEnablersCounter;
    if (this.EnablersSuspended())
      return;
    this.UpdateAllEnablers();
  }

  private void UpdateAllEnablers()
  {
    if (this.EnablersSuspended())
      return;
    this.EnalbeOKButton();
  }

  private void EnalbeOKButton()
  {
    if (this.EnablersSuspended())
      return;
    this.m_okButton.Enabled = this.m_name.Text.Trim().Length > 0 && this.m_comElement.Text != "";
  }

  protected virtual void Dispose(bool disposing)
  {
    if (disposing && this.components != null)
      this.components.Dispose();
    base.Dispose(disposing);
  }

  private void InitializeComponent()
  {
    ComponentResourceManager componentResourceManager = new ComponentResourceManager(typeof (CUiPLCSimitCouplingConnectionForm));
    this.m_okButton = new Button();
    this.label1 = new Label();
    this.m_hostNameLabel = new Label();
    this.m_name = new TextBox();
    this.m_IP = new TextBox();
    this.m_cancelButton = new Button();
    this.label3 = new Label();
    this.m_prefixLabel = new Label();
    this.m_prefix = new TextBox();
    this.m_infoBoxPrefix = new TxInfoButton();
    this.m_comElement = new TextBox();
    this.m_infoBoxComElement = new TxInfoButton();
    this.m_infoBoxHostName = new TxInfoButton();
    ((Control) this).SuspendLayout();
    componentResourceManager.ApplyResources((object) this.m_okButton, "m_okButton");
    this.m_okButton.Name = "m_okButton";
    this.m_okButton.UseVisualStyleBackColor = true;
    this.m_okButton.Click += new EventHandler(this.OK_Click);
    componentResourceManager.ApplyResources((object) this.label1, "label1");
    this.label1.Name = "label1";
    componentResourceManager.ApplyResources((object) this.m_hostNameLabel, "m_hostNameLabel");
    this.m_hostNameLabel.Name = "m_hostNameLabel";
    componentResourceManager.ApplyResources((object) this.m_name, "m_name");
    this.m_name.Name = "m_name";
    this.m_name.TextChanged += new EventHandler(this.m_name_TextChanged);
    componentResourceManager.ApplyResources((object) this.m_IP, "m_IP");
    this.m_IP.Name = "m_IP";
    this.m_IP.ReadOnly = true;
    componentResourceManager.ApplyResources((object) this.m_cancelButton, "m_cancelButton");
    this.m_cancelButton.DialogResult = DialogResult.Cancel;
    this.m_cancelButton.Name = "m_cancelButton";
    this.m_cancelButton.UseVisualStyleBackColor = true;
    this.m_cancelButton.Click += new EventHandler(this.m_cancelButton_Click);
    componentResourceManager.ApplyResources((object) this.label3, "label3");
    this.label3.Name = "label3";
    componentResourceManager.ApplyResources((object) this.m_prefixLabel, "m_prefixLabel");
    this.m_prefixLabel.Name = "m_prefixLabel";
    componentResourceManager.ApplyResources((object) this.m_prefix, "m_prefix");
    this.m_prefix.Name = "m_prefix";
    componentResourceManager.ApplyResources((object) this.m_infoBoxPrefix, "m_infoBoxPrefix");
    ((Control) this.m_infoBoxPrefix).Name = "m_infoBoxPrefix";
    this.m_infoBoxPrefix.TabStop = false;
    componentResourceManager.ApplyResources((object) this.m_comElement, "m_comElement");
    this.m_comElement.Name = "m_comElement";
    this.m_comElement.TextChanged += new EventHandler(this.m_comElement_TextChanged);
    componentResourceManager.ApplyResources((object) this.m_infoBoxComElement, "m_infoBoxComElement");
    ((Control) this.m_infoBoxComElement).Name = "m_infoBoxComElement";
    this.m_infoBoxComElement.TabStop = false;
    componentResourceManager.ApplyResources((object) this.m_infoBoxHostName, "m_infoBoxHostName");
    ((Control) this.m_infoBoxHostName).Name = "m_infoBoxHostName";
    this.m_infoBoxHostName.TabStop = false;
    ((Form) this).AcceptButton = (IButtonControl) this.m_okButton;
    componentResourceManager.ApplyResources((object) this, "$this");
    ((ContainerControl) this).AutoScaleDimensions = new SizeF(6f, 13f);
    ((ContainerControl) this).AutoScaleMode = AutoScaleMode.Font;
    ((Form) this).CancelButton = (IButtonControl) this.m_cancelButton;
    ((Control) this).Controls.Add((Control) this.m_infoBoxHostName);
    ((Control) this).Controls.Add((Control) this.m_infoBoxComElement);
    ((Control) this).Controls.Add((Control) this.m_comElement);
    ((Control) this).Controls.Add((Control) this.m_infoBoxPrefix);
    ((Control) this).Controls.Add((Control) this.m_prefix);
    ((Control) this).Controls.Add((Control) this.m_prefixLabel);
    ((Control) this).Controls.Add((Control) this.label3);
    ((Control) this).Controls.Add((Control) this.m_cancelButton);
    ((Control) this).Controls.Add((Control) this.m_IP);
    ((Control) this).Controls.Add((Control) this.m_name);
    ((Control) this).Controls.Add((Control) this.m_hostNameLabel);
    ((Control) this).Controls.Add((Control) this.label1);
    ((Control) this).Controls.Add((Control) this.m_okButton);
    ((Form) this).MaximizeBox = false;
    ((Form) this).MinimizeBox = false;
    ((Control) this).Name = nameof (CUiPLCSimitCouplingConnectionForm);
    ((Form) this).ShowIcon = false;
    ((Form) this).SizeGripStyle = SizeGripStyle.Hide;
    ((Form) this).FormClosing += new FormClosingEventHandler(this.Action_FormClosing);
    ((Control) this).ResumeLayout(false);
    ((Control) this).PerformLayout();
  }
}
